package com.infonam.jsf.eshop.execute;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

import com.infonam.jsf.eshop.model.Product;

public class OnDetailActionListener implements ActionListener {

	@Override
	public void processAction(ActionEvent ev) throws AbortProcessingException {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		Product p = app.evaluateExpressionGet(context, "#{p}",com.infonam.jsf.eshop.model.Product.class );
		System.out.println(p.getId());
		
		//First way to store Product value.
		/* 
		ProductHolder ph = app.evaluateExpressionGet(context, "#{ph}",com.infonam.jsf.eshop.execute.ProductHolder.class );
		ph.setCurrentProduct(p);
		*/
		//Second way to store Product value.
		ELContext elContext = context.getELContext();
		ValueExpression ve=app.getExpressionFactory().createValueExpression(elContext, "#{ph.currentProduct}", com.infonam.jsf.eshop.model.Product.class);
		ve.setValue(elContext, p);
		
	}

}
